using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UpgradeProjectileDiversity:Upgrade {

	Dictionary<GameObject,float> timeLastShoot = new Dictionary<GameObject,float>();
	static HashSet<GameObject> removeBuffer = new HashSet<GameObject>();
	const float checkDuration = 3f;

	protected override void BurstDecidingEventHandler(object _sender,BurstDecidingEventArgs e) {
		base.BurstDecidingEventHandler(_sender,e);
		e.stat.cooldownTime.AddDivideAdd(Mathf.Max(0,timeLastShoot.Count-1)*0.1f,0);
	}
	protected override void ProjectileFiringEventHandler(object _sender,ProjectileEventArgs e) {
		base.ProjectileFiringEventHandler(_sender,e);
		Projectile sender = _sender as Projectile;
		if(timeLastShoot.ContainsKey(sender.prefab)) timeLastShoot[sender.prefab]=Time.time;
		else timeLastShoot.Add(sender.prefab,Time.time);
	}

	public override void Update(object sender,WeaponEventArgs e) {
		base.Update(sender,e);
		removeBuffer.Clear();
		foreach(var i in timeLastShoot) {
			if(i.Value<Time.time-checkDuration) removeBuffer.Add(i.Key);
		}
		foreach(var i in removeBuffer) timeLastShoot.Remove(i);
		AddProgression(1-Mathf.Pow(0.9f,Mathf.Max(0,timeLastShoot.Count-1)));
	}

}
